import sys
from PyQt5.QtWidgets import QSlider, QApplication, QVBoxLayout, QHBoxLayout, QWidget, QLabel
from PyQt5.QtCore import Qt

class SignalHandling(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.setup_signals()
    
    def setup_ui(self):
        layout = QVBoxLayout()
        
        self.slider = QSlider(Qt.Horizontal)
        self.slider.setRange(0, 100)
        self.slider.setValue(50)
        
        self.value_label = QLabel("当前值: 50")
        
        layout.addWidget(self.slider)
        layout.addWidget(self.value_label)
        self.setLayout(layout)
    
    def setup_signals(self):
        # valueChanged 信号 - 数值改变时发射
        self.slider.valueChanged.connect(self.on_value_changed)
        
        # sliderPressed 信号 - 用户开始拖动滑块时发射
        self.slider.sliderPressed.connect(self.on_slider_pressed)
        
        # sliderMoved 信号 - 用户拖动滑块时实时发射
        self.slider.sliderMoved.connect(self.on_slider_moved)
        
        # sliderReleased 信号 - 用户释放滑块时发射
        self.slider.sliderReleased.connect(self.on_slider_released)
        
        # rangeChanged 信号 - 数值范围改变时发射
        self.slider.rangeChanged.connect(self.on_range_changed)
    
    def on_value_changed(self, value):
        """数值改变信号处理"""
        self.value_label.setText(f"当前值: {value}")
        print(f"数值改变: {value}")
    
    def on_slider_pressed(self):
        """滑块按下信号处理"""
        print("开始拖动滑块")
        self.value_label.setStyleSheet("color: red;")
    
    def on_slider_moved(self, value):
        """滑块移动信号处理"""
        print(f"滑块移动中: {value}")
    
    def on_slider_released(self):
        """滑块释放信号处理"""
        print("滑块释放")
        self.value_label.setStyleSheet("color: black;")
        final_value = self.slider.value()
        print(f"最终值: {final_value}")
    
    def on_range_changed(self, min_val, max_val):
        """范围改变信号处理"""
        print(f"范围改变: {min_val} - {max_val}")




if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SignalHandling()
    window.show()
    sys.exit(app.exec_())